package cn.gz.lbs.glsc;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.gz.lbs.main.SPRequestManager;
import cn.gz.zoomtech.common.ConfManager;
import cn.gz.zoomtech.common.db.DbHandle;


import test.Test;

/**
 * Servlet implementation class LbsProxyServer
 */
public class GLSCProxyServerDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 private static final Log log = LogFactory.getLog(GLSCProxyServerDemo.class);
	 private static CompositeConfiguration cf = ConfManager.getInstance().getConf();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GLSCProxyServerDemo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		  step0: 客户端IP信息：
		  String clientIp = request.getRemoteHost();
		  log.info("IamGLSCServer,clientIp:"+clientIp);
//        step1: 读取客户端请求,输出请求字符串
	      InputStreamReader reader = new InputStreamReader(request.getInputStream(), "utf-8"); 
	      char[] buff = new char[1024]; 
	      int length = 0; 
	      String reqStr = "";
	      while ((length = reader.read(buff)) != -1) { 
	    	  reqStr = reqStr + new String(buff, 0, length); 
	      }
	      log.info("IamGLSCServer,clientReqStr:"+reqStr);
	      
	      
	    GLSCResponseManager glscRespM = new GLSCResponseManager();
	    
		String respStr = glscRespM.getGlscResp(reqStr);
//      step2: 返回响应给客户端
		response.setContentType("text/xml"); 
		response.setCharacterEncoding("utf-8"); 
        PrintWriter out = response.getWriter(); 
        out.println(respStr); 	
//		log.info("deal post OK...........respStr:"+respStr);
		
	}

}
